//
//  MineViewController.m
//  cityCar
//
//  Created by yao on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MineViewController.h"
#import "MineCollectViewController.h"
#import "ForumGather.h"
#import "ForumDetailViewController.h"
#import "SeekWebViewController.h"
#import <Comment/Comment.h>
#import "MineFriendViewController.h"
#import <SinaWeiboConnection/SSSinaWeiboUser.h>

@interface MineViewController ()

@property(nonatomic, retain)UIImageView *myImageView;
@property(nonatomic, retain)UIButton *enterButton;
@property(nonatomic, retain)UIButton *forumButton;
@property(nonatomic, retain)UIButton *collectButton;
@property(nonatomic, retain)UIButton *systemButton;

// 最近访问的数组
@property(nonatomic, retain)NSMutableArray *AllArray;
@property(nonatomic, retain)NSMutableArray *carArray;

@end

@implementation MineViewController

- (void)dealloc
{
    [_myImageView release];
    [_enterButton release];
    [_forumButton release];
    [_collectButton release];
    [_systemButton release];
    [_AllArray release];
    [_carArray release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.AllArray = [NSMutableArray array];
        self.carArray = [NSMutableArray array];
    }
    return self;
}
- (void)setupViews
{
    // 设置标题
    self.title = @"个人中心";
    self.view.backgroundColor = [WebColor whiteSmoke];
    
    
    // 视图控制器的宽和高
    CGFloat x = self.view.frame.size.width;
    //    CGFloat y = self.view.frame.size.height;
    self.navigationController.navigationBar.translucent = YES;
    
    // 背景图片
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, x, 250)];
    bgImageView.image = [UIImage imageNamed:@"beijingtupian.png"];
    [self.view addSubview:bgImageView];
    [bgImageView release];
    
    // 头像
    // 圆角的imageView
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 100, 50, 50)];
    self.myImageView.clipsToBounds = YES;
    self.myImageView.layer.cornerRadius = 25;
    self.myImageView.image = [UIImage imageNamed:@"111.jpg"];
    [self.view addSubview:self.myImageView];
    [_myImageView release];
    
    // 点击登录按钮
    self.enterButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.enterButton.frame = CGRectMake(90, 110, 130, 30);
    UIFont *font = [UIFont fontWithName:@"MarkerFelt-Thin" size:18];
    self.enterButton.titleLabel.font = font; 
    [self.enterButton setTitle:@"点击登录" forState:UIControlStateNormal];
    [self.enterButton setTitleColor:[WebColor black] forState:UIControlStateNormal];
    [self.enterButton addTarget:self action:@selector(enterButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.enterButton];
    // 双击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTapsRequired = 2;
    [self.enterButton addGestureRecognizer:tap];
    [tap release];
    
    // 我的论坛按钮
    self.forumButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.forumButton.frame = CGRectMake(30, 180, 50, 30);
    self.forumButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.forumButton setImage:[UIImage imageNamed:@"forum32.png"] forState:UIControlStateNormal];
    self.forumButton.tintColor = [WebColor dimGray];
    [self.forumButton addTarget:self action:@selector(mineCollectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.forumButton.tag = 10011;
    [self.view addSubview:self.forumButton];
    
    UILabel *forumLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 210, 60, 30)];
    forumLabel.text = @"我的论坛";
    forumLabel.font = [UIFont systemFontOfSize:15];
    forumLabel.textColor = [WebColor black];
    [bgImageView addSubview:forumLabel];
    [forumLabel release];
    
    // 我的收藏按钮
    self.collectButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.collectButton.frame = CGRectMake(145, 180, 50, 30);
    self.collectButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.collectButton setImage:[UIImage imageNamed:@"iconfont-shoucang-3.png"] forState:UIControlStateNormal];
    self.collectButton.tintColor = [WebColor dimGray];
    [self.collectButton addTarget:self action:@selector(mineCollectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.collectButton.tag = 10010;
    [self.view addSubview:self.collectButton];
    
    UILabel *collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 210, 60, 30)];
    collectLabel.text = @"我的收藏";
    collectLabel.font = [UIFont systemFontOfSize:15];
    collectLabel.textColor = [WebColor black];
    [self.view addSubview:collectLabel];
    [collectLabel release];
    
    
    // 我的好友
    self.systemButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.systemButton.frame = CGRectMake(250, 180, 50, 30);
    self.systemButton.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.systemButton setImage:[UIImage imageNamed:@"iconfont-wodeguanzhu.png"] forState:UIControlStateNormal];
    self.systemButton.tintColor = [WebColor dimGray];
    [self.systemButton addTarget:self action:@selector(friendButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.systemButton.tag = 10012;
    [self.view addSubview:self.systemButton];
    
    UILabel *systemLabel = [[UILabel alloc] initWithFrame:CGRectMake(245, 210, 60, 30)];
    systemLabel.text = @"我的好友";
    systemLabel.font = [UIFont systemFontOfSize:15];
    systemLabel.textColor = [WebColor black];
    [self.view addSubview:systemLabel];
    [systemLabel release];
    
    // 最近访问
    UIImageView *imageViewRecent = [[UIImageView alloc] initWithFrame:CGRectMake(20, 280, 20, 20)];
    
//    UIButton *recentlyButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    recentlyButton.frame = CGRectMake(20, 280, 20, 20);
    [imageViewRecent setImage:[UIImage imageNamed:@"iconfont-ttpodicon.png"]];
    [self.view addSubview:imageViewRecent];
    [imageViewRecent release];
    UILabel *recentlyLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 280, 80, 20)];
    recentlyLabel.text = @"最近访问";
    [self.view addSubview:recentlyLabel];
    [recentlyLabel release];
    
    // 论坛
    UILabel *recently_ForumLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 310, 20, 40)];
    recently_ForumLabel.text = @"论坛";
    recently_ForumLabel.font = [UIFont systemFontOfSize:15];
    recently_ForumLabel.numberOfLines = 0;
    recently_ForumLabel.textColor = [WebColor white];
    recently_ForumLabel.backgroundColor = [WebColor dimGray];
    [self.view addSubview:recently_ForumLabel];
    [recently_ForumLabel release];
    
    // 车系
    UILabel *recently_CarLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 360, 20, 40)];
    recently_CarLabel.text = @"车系";
    recently_CarLabel.font = [UIFont systemFontOfSize:15];
    recently_CarLabel.numberOfLines = 0;
    recently_CarLabel.textColor = [WebColor white];
    recently_CarLabel.backgroundColor = [WebColor dimGray];
    [self.view addSubview:recently_CarLabel];
    [recently_CarLabel release];
    
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
}

- (void)viewDidAppear:(BOOL)animated
{
    self.leftButton.hidden = NO;
    self.rightButton.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;

    //    self.AllArray = [NSMutableArray array];
    
    self.AllArray = [NSMutableArray arrayWithArray:[self achieveDate]];
    
    for (int i = self.AllArray.count - 1; i >= 0; i--) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(55 + (self.AllArray.count - 1 - i) * 65, 310 , 55, 40);
        button.titleLabel.font = [UIFont systemFontOfSize:11];
        button.titleLabel.numberOfLines = 0;
        ForumGather *forum = [self.AllArray objectAtIndex:i];
        [button setTitle:forum.name forState:UIControlStateNormal];
        [button addTarget:self action:@selector(forumButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 30000 + i;
        button.backgroundColor = [WebColor white];
        button.tintColor = [WebColor black];
        [self.view addSubview:button];

    }
    
    
    self.carArray = [NSMutableArray arrayWithArray:[self achieveDateCar]];
    
    for (int i = self.carArray.count - 1; i >= 0; i--) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(55 + (self.carArray.count - 1 - i) * 65, 360 , 55, 40);
        button.titleLabel.font = [UIFont systemFontOfSize:11];
        button.titleLabel.numberOfLines = 0;
        ForumGather *forum = [self.carArray objectAtIndex:i];
        [button setTitle:forum.name forState:UIControlStateNormal];
        [button addTarget:self action:@selector(forumButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 31000 + i;
        button.backgroundColor = [WebColor white];
        button.tintColor = [WebColor black];
        [self.view addSubview:button];
    }
    
    
}

// 从本地获取数据(论坛)
- (NSArray *)achieveDate
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths lastObject];
    NSArray *achieveArray = [NSArray array];
    NSString *carPath = [documentPath stringByAppendingPathComponent:@"recently.aaa"];
    achieveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:carPath];
    
    return achieveArray;
    
}

// 从本地获取数据(车系)
- (NSArray *)achieveDateCar
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [paths lastObject];
    NSArray *achieveArray = [NSArray array];
    NSString *carPath = [documentPath stringByAppendingPathComponent:@"recentlyCar.aaa"];
    achieveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:carPath];
    return achieveArray;
}


// 点击最近浏览的按钮
- (void)forumButtonClicked:(UIButton *)button
{
    if (button.tag >= 30000 && button.tag <= 30010) {
        ForumDetailViewController *detailVC = [[ForumDetailViewController alloc] init];
        
        NSString *strId = [NSString string];
        for (ForumGather *forum in self.AllArray) {
            if ([button.titleLabel.text isEqualToString:forum.name]) {
                strId = forum.Id;
                break;
            }
        }
        detailVC.dic = @{@"Name": button.titleLabel.text};
        detailVC.string = strId;
        [self presentViewController:detailVC animated:YES completion:^{
            
        }];
    } else {
        
        SeekWebViewController *seekWebVC = [[SeekWebViewController alloc] init];
        NSString *strId = [NSString string];
        for (ForumGather *forum in self.carArray) {
            if ([button.titleLabel.text isEqualToString:forum.name]) {
                strId = forum.Id;
                break;
            }
        }
        NSMutableString *string = [NSMutableString stringWithFormat:@"http://price.pcauto.com.cn/sg%@/", strId];
        seekWebVC.Idurl = string;
        [self presentViewController:seekWebVC animated:YES completion:^{
            
        }];
        
    }
}


// 点击登录的方法
- (void)enterButtonClicked:(UIButton *)button
{
    if ([self.enterButton.titleLabel.text isEqualToString:@"点击登录"]) {
        [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
            if (result) {
                UIAlertView* alverView = [[UIAlertView alloc]initWithTitle:nil message:@"登录成功" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
                [alverView show];
                
                [self.myImageView sd_setImageWithURL:[NSURL URLWithString:[userInfo profileImage]]];
                [self.enterButton setTitle:[userInfo nickname] forState:UIControlStateNormal];
                [alverView release];
            }else{
                
                UIAlertView* alverView = [[UIAlertView alloc]initWithTitle:nil message:@"登录失败" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
                [alverView show];
                [alverView release];
            }
            
        }];
        
    } else {
        
        // 显示连击2下注销登录信息
        UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 170) / 2, 450, 170, 30)];
        Label.text = @"连击2下注销登录哦!";
        Label.textColor = [WebColor white];
        Label.layer.cornerRadius = 10;
        Label.clipsToBounds = YES;
        Label.backgroundColor = [WebColor black];
        Label.alpha = 0.7;
        Label.textAlignment = NSTextAlignmentCenter;
        Label.tag = 158;
        [self.view addSubview:Label];
        [self.view bringSubviewToFront:Label];
        [Label release];
        
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(cartoon) userInfo:nil repeats:NO];
        
    }
}

- (void)cartoon
{
    UILabel *label = (UILabel *)[self.view viewWithTag:158];
    [UIView animateWithDuration:2 animations:^{
        label.alpha = 0.1;
    } completion:^(BOOL finished) {
        [label removeFromSuperview];
        label.alpha = 0.7;
    }];
    
}


// 注销登录
- (void)tapAction:(UIButton *)button
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否注销登录？" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
    [alert release];
}
#pragma mark - alertView 的协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
        self.myImageView.image = [UIImage imageNamed:@"111.jpg"];
        [self.enterButton setTitle:@"点击登录" forState:UIControlStateNormal];
    }
    
}


// 点击我的收藏和我的论坛按钮
- (void)mineCollectButtonClicked:(UIButton *)button
{
    
    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] init]autorelease];
    if (button.tag == 10010) {
        backItem.title = @"我的收藏";
    } else if (button.tag == 10011) {
        backItem.title = @"我的论坛";
    }
    backItem.tintColor = [WebColor black];
    self.navigationItem.backBarButtonItem = backItem;
    
    self.leftButton.hidden = YES;
    self.rightButton.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    MineCollectViewController *collectVC = [[MineCollectViewController alloc] init];
    collectVC.temp = button.tag;
    [self.navigationController pushViewController:collectVC animated:YES];

    
    
}

// 点击我的好友按钮
- (void)friendButtonClicked:(UIButton *)button
{
    if ([self.enterButton.titleLabel.text isEqualToString:@"点击登录"]) {
        UIAlertView* alverView = [[UIAlertView alloc]initWithTitle:nil message:@"请登录^_^" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
        [alverView show];
    } else {

        [ShareSDK getFriendsWithType:ShareTypeSinaWeibo page:nil authOptions:nil result:^(SSResponseState state, NSArray *users, long long curr, long long prev, long long next, BOOL hasNext, NSDictionary *extInfo, id<ICMErrorInfo> error) {
            
            MineFriendViewController *friendVC = [[MineFriendViewController alloc] init];
            friendVC.friendArr = users;
            [self.navigationController setNavigationBarHidden:YES animated:YES];
            [self.tabBarController.tabBar setHidden:YES];
            [self.navigationController pushViewController:friendVC animated:YES];
            
        }];
  
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

